package com.itheima.case2.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.case2.pojo.Role;
import com.itheima.case2.serviceiterface.RoleService;
import com.itheima.case2.utils.BaseController;
import com.itheima.case2.utils.BeansFactory;
import com.itheima.case2.vo.Result;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//http://localhost:8080/role?methodName=findAllRoles
@WebServlet("/role")
public class RoleServlet extends BaseServlet {

    //查询所有角色信息返回给浏览器
    public void findAllRoles(HttpServletRequest request, HttpServletResponse response) {
        //在该方法体中处理请求和响应
        //获取请求参数
        String methodName = request.getParameter("methodName");
        //
        //创建service层对象
        RoleService roleService = (RoleService) BeansFactory.getSingleBean("roleService");
        //调用roleService的查询所有角色的方法
        List<Role> allRoles = roleService.findAllRoles();
        //响应数据给前端
        //4.响应数据给前端
        //BaseController.printResult(response,new Result(true,"分页查询成功",pb));"
        try {
            BaseController.printResult(response,new Result(true,"查询成功",allRoles));
        } catch (IOException e) {
            e.printStackTrace();
            try {
                //查询失败，响应给前端
                BaseController.printResult(response,new Result(false,"查询失败"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }

}
